

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;

namespace JTC.MiniCommerce.Web.admin {
  public partial class errorlogs : System.Web.UI.Page {

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        SetErrorLogsProperties();
        LoadErrorLogs();
      }
      catch (Exception ex) {
        Logger.Error(typeof(errorlogs).Name + ".Page_Load", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnDeleteAll control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnDeleteAll_Click(object sender, EventArgs e) {
      try {
        LogController.DeleteAllLogs();
        LoadErrorLogs();
        Master.MessageCenter.DisplaySuccessMessage(LocalizationUtility.GetText("lblDeleteAllLogs"));
      }
      catch (Exception ex) {
        Logger.Error(typeof(errorlogs).Name + ".btnDeleteAll_Click", ex);
        Master.MessageCenter.DisplayCriticalMessage(string.Format(LocalizationUtility.GetText("lblCriticalError"), ex.Message));
      }
    }

    /// <summary>
    /// Handles the Paging event of the dgErrorLogListing control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridPageChangedEventArgs"/> instance containing the event data.</param>
    public void dgErrorLogListing_Paging(object sender, DataGridPageChangedEventArgs e) {
      dgErrorLogListing.CurrentPageIndex = e.NewPageIndex;
      LoadErrorLogs();
    }

    /// <summary>
    /// Handles the ItemDataBound event of the dgErrorLogListing control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridItemEventArgs"/> instance containing the event data.</param>
    void dgErrorLogListing_ItemDataBound(object sender, DataGridItemEventArgs e) {
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        e.Item.Attributes.Add("onmouseover", "this.className = \"over\";");
        e.Item.Attributes.Add("onmouseout", "this.className = this.className.replace(\"over\", \"\");");
      }
    }

    #endregion

    #region Methods

    #region Private

    /// <summary>
    /// Sets the error logs properties.
    /// </summary>
    private void SetErrorLogsProperties() {
      LocalizationUtility.AddLabelText(lblErrorLogListing);
      LocalizationUtility.AddPanelText(pnlCurrentErrorLogs);
      LocalizationUtility.AddButtonText(btnDeleteAll);
      this.Title = LocalizationUtility.GetText("titleErrorLogs");
    }

    /// <summary>
    /// Loads the error logs.
    /// </summary>
    private void LoadErrorLogs() {
      LogCollection logCollection = new LogController().FetchAllLogs();
      if(logCollection.Count > 0) {
        dgErrorLogListing.DataSource = logCollection;
        dgErrorLogListing.ItemDataBound += new DataGridItemEventHandler(dgErrorLogListing_ItemDataBound);
        dgErrorLogListing.Columns[1].HeaderText = LocalizationUtility.GetText("hdrDetails");
        dgErrorLogListing.Columns[2].HeaderText = LocalizationUtility.GetText("hdrLogDate");
        dgErrorLogListing.Columns[3].HeaderText = LocalizationUtility.GetText("hdrLogLevel");
        dgErrorLogListing.Columns[4].HeaderText = LocalizationUtility.GetText("hdrLogMessage");
        HyperLinkColumn hlDetailsColumn = dgErrorLogListing.Columns[1] as HyperLinkColumn;
        if(hlDetailsColumn != null) {
          hlDetailsColumn.Text = LocalizationUtility.GetText("lblDetails");
        }
        dgErrorLogListing.DataBind();
      }
      else {
        pnlCurrentErrorLogs.Visible = false;
        Master.MessageCenter.DisplayInformationMessage(LocalizationUtility.GetText("lblNoErrorLogs"));
      }
    }

    #endregion

    #endregion

  }
}
